 
package com.zdkj.subway.stm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.subway.stm.entity.SwStInvoiceBalance;
import com.zdkj.subway.stm.entity.SwStInvoiceUse;
import com.zdkj.subway.stm.mapper.SwStInvoiceUseMapper;
import com.zdkj.subway.stm.service.SwStInvoiceBalanceService;
import com.zdkj.subway.stm.service.SwStInvoiceUseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车站发票使用台账
 *
 * @author zdkj code generator
 * @date 2020-05-25 10:42:55
 */
@Service
@AllArgsConstructor
public class SwStInvoiceUseServiceImpl extends ServiceImpl<SwStInvoiceUseMapper, SwStInvoiceUse> implements SwStInvoiceUseService {

    private SwStInvoiceUseMapper swStInvoiceUseMapper;
    private SwStInvoiceBalanceService swStInvoiceBalanceService;



    @Override
    public boolean timerSave(SwStInvoiceUse swStInvoiceUse) {
        //哪天没有领用的话也就不存在结存。
        List<SwStInvoiceBalance> list = swStInvoiceUseMapper.listGroupBy(swStInvoiceUse);
        if (list!=null){
            list.forEach(a->{
                /**
                 * 查询当天同一发票类型 面额、车站的发票领用list
                 */
                List<SwStInvoiceUse> slist = swStInvoiceUseMapper.selStartEnd(a);
                /**
                 * 查询  同一车站、发票类型、发票面额的最后一条结存记录
                 *
                 */
                SwStInvoiceBalance isExisit = swStInvoiceBalanceService.getOne(Wrappers.query(new SwStInvoiceBalance()).lambda()
                        .eq(SwStInvoiceBalance::getInvoiceMoney, a.getInvoiceMoney())
                        .eq(SwStInvoiceBalance::getInvoiceType, a.getInvoiceType())
                        .eq(SwStInvoiceBalance::getStationCode, a.getStationCode())
                        .orderByDesc(SwStInvoiceBalance::getStartEndDate).last("LIMIT 1")
                );
                a.setInvoiceStart(slist.get(0).getInvoiceStart());
                /**
                 * 如果存在拿出发票起号设置为新纪录的起号
                 */
                if (isExisit!=null){
                    a.setInvoiceStart(isExisit.getInvoiceStart());
                }
                a.setInvoiceEnd(slist.get(slist.size()-1).getInvoiceEnd());
                /**
                 * 查询同一日期、车站、发票类型、发票面额的记录
                 */
                SwStInvoiceBalance one = swStInvoiceBalanceService.getOne(Wrappers.query(new SwStInvoiceBalance()).lambda()
                        .eq(SwStInvoiceBalance::getInvoiceMoney, a.getInvoiceMoney())
                        .eq(SwStInvoiceBalance::getInvoiceType, a.getInvoiceType())
                        .eq(SwStInvoiceBalance::getStartEndDate, a.getStartEndDate())
                        .eq(SwStInvoiceBalance::getStationCode, a.getStationCode())
                );
                /**
                 * 不存在新增
                 */
                if (one==null){
                    swStInvoiceBalanceService.save(a);
                }else {
                    /**
                     * 存在更新   正常走定时任务不会到这个流程，
                     * 此处是应对：假如页面上点击了统计结存。那么定时任务执行到的时候就覆盖。
                     * 定时任务是统计前一天的
                     */
                    a.setId(one.getId());
                    swStInvoiceBalanceService.updateById(a);
                }
            });
        }

        return true;
    }

}
